import { Menu, Tray, app, nativeImage, globalShortcut } from 'electron';
import icon from '../../resources/icon.png?asset'
import { openMainWin, goHomeMainWin, showHideMainWin,rememberType } from './mainWin';
import { opensettingWin, goHomesettingWin, showHidesettingWin } from './setting';
import { openaboutWin, goHomeaboutWin, showHideaboutWin } from './aboutWin';

import { readConfig, writeConfig, readFile } from './utils.js'

var tray
export function initTray() {
    const trayIcon = nativeImage.createFromPath(icon)
    tray = new Tray(trayIcon)
    updateContextMenu()

    tray.on('double-click', () => {
        showHideMainWin()
    })
    tray.setToolTip('YangShaoTool');
    globalShortcut.registerAll(['Alt+/'], () => {
        rememberType()
    })
    globalShortcut.registerAll(['Alt+numdec', 'Alt+.', 'Alt+x'], () => {
        showHideMainWin()
    })
    globalShortcut.registerAll(['Alt+0', 'Alt+Num0'], () => {
        setMode({ checkLabel: 'normal' })
        updateContextMenu()
    })
    globalShortcut.registerAll(['Alt+1', 'Alt+Num1'], () => {
        setMode({ checkLabel: 'todo' })
        updateContextMenu()
    })
    globalShortcut.registerAll(['Alt+2', 'Alt+Num2'], () => {
        setMode({ checkLabel: 'read' })
        updateContextMenu()
    })
    globalShortcut.registerAll(['Alt+3', 'Alt+Num3'], () => {
        setMode({ checkLabel: 'color' })
        updateContextMenu()
    })
    globalShortcut.registerAll(['Alt+4', 'Alt+Num4'], () => {
        setMode({ checkLabel: 'screenShort' })
        updateContextMenu()
    })
    globalShortcut.registerAll(['Alt+5', 'Alt+Num5'], () => {
        setMode({ checkLabel: 'online' })
        updateContextMenu()
    })
    globalShortcut.registerAll(['Alt+6', 'Alt+Num6'], () => {
        setMode({ checkLabel: 'notes' })
        updateContextMenu()
    })
    globalShortcut.registerAll(['Alt+7', 'Alt+Num7'], () => {
        setMode({ checkLabel: 'music' })
        updateContextMenu()
    })
    globalShortcut.registerAll(['Alt+8', 'Alt+Num8'], () => {
        setMode({ checkLabel: 'measure' })
        updateContextMenu()
    })
    globalShortcut.registerAll(['Alt+9', 'Alt+Num9'], () => {
        setMode({ checkLabel: 'screenRecorder' })
        updateContextMenu()
    })
    globalShortcut.registerAll(['Ctrl+1', 'Ctrl+Num1'], () => {
        setMode({ checkLabel: 'translate' })
        updateContextMenu()
    })
    globalShortcut.registerAll(['Ctrl+2', 'Ctrl+Num2'], () => {
        setMode({ checkLabel: 'getWords' })
        updateContextMenu()
    })
    
    // 在应用即将退出时，销毁托盘图标
    app.on('before-quit', () => {
        if (tray) {
            tray.destroy()
        }
    })
    return tray
}

const modeConfigs = {
    normal: { label: 'normal', url: '/menu' },
    todo: { label: 'todo', url: '/todo' },
    read: { label: 'read', url: '/read' },
    color: { label: 'color', url: '/color' },
    screenShort: { label: 'screenShort', url: '/screenShort' },
    online: { label: 'online', url: '/online' },
    notes: { label: 'notes', url: '/notes' },
    music: { label: 'music', url: '/music' },
    measure: { label: 'measure', url: '/measure' },
    screenRecorder: { label: 'screenRecorder', url: '/screenRecorder' },
    translate: { label: 'translate', url: '/translate' },
    getWords: { label: 'getWords', url: '/getWords' },
};

const setMode = (mode) => {
    const config = readConfig() || {};
    config.mode = modeConfigs[mode.checkLabel];
    if (config.mode) {
        writeConfig(config);
        goHomeMainWin();
    }
};
export const setModes = (mode) => {
    const config = readConfig() || {};
    config.mode = modeConfigs[mode.checkLabel];
    if (config.mode) {
        writeConfig(config);
    }
}
export const updateContextMenu = () => {
    const contextMenu = Menu.buildFromTemplate([
        {
            label: '首页', checkLabel: 'normal', type: 'radio', checked: isChecked('normal'), click: (item) => {
                setMode(item)
            }
        },
        {
            label: 'Todo模式', checkLabel: 'todo', type: 'radio', checked: isChecked('todo'), click: (item) => {
                setMode(item)
            }
        },
        {
            label: '阅读模式', checkLabel: 'read', type: 'radio', checked: isChecked('read'), click: (item) => {
                setMode(item)
            }
        },
        {
            label: '取色器', checkLabel: 'color', type: 'radio', checked: isChecked('color'), click: (item) => {
                setMode(item)
            }
        },
        {
            label: '截图模式', checkLabel: 'screenShort', type: 'radio', checked: isChecked('screenShort'), click: (item) => {
                setMode(item)
            }
        },
        {
            label: '联机模式', checkLabel: 'online', type: 'radio', checked: isChecked('online'), click: (item) => {
                setMode(item)
            }
        },
        {
            label: '笔记模式', checkLabel: 'notes', type: 'radio', checked: isChecked('notes'), click: (item) => {
                setMode(item)
            }
        },
        {
            label: '音乐模式', checkLabel: 'music', type: 'radio', checked: isChecked('music'), click: (item) => {
                setMode(item)
            }
        },
        {
            label: '测量模式', checkLabel: 'measure', type: 'radio', checked: isChecked('measure'), click: (item) => {
                setMode(item)
            }
        },
        {
            label: '录屏模式', checkLabel: 'screenRecorder', type: 'radio', checked: isChecked('screenRecorder'), click: (item) => {
                setMode(item)
            }
        },
        {
            label: '翻译模式', checkLabel: 'translate', type: 'radio', checked: isChecked('translate'), click: (item) => {
                setMode(item)
            }
        },
        {
            label: '图识字', checkLabel: 'getWords', type: 'radio', checked: isChecked('getWords'), click: (item) => {
                setMode(item)
            }
        },
        // {
        //     label: '设置',
        //     click: function () {
        //         opensettingWin()
        //     }
        // },
        {
            label: '重启',
            click: function () {
                app.relaunch();
                app.exit(0);
                openMainWin()
            }
        },
        {
            label: '帮助',
            click: function () {
                openaboutWin()
            }
        },
        {
            label: '退出',
            click: function () {
                app.exit()
            }
        }
    ])
    tray.setContextMenu(contextMenu);
}
const isChecked = (label) => {
    let flag = readConfig().mode.label === label ? true : false
    return flag
}